<html>
<head><title>Calculator</title></head>
<body>

<h1>Calculator</h1>
Alexander Lee, Ian Yarbrough<br />
Type an expression below
<p>
<form action="calculator.php" method="GET">
<input type="text" name="expression" />
<input type="submit" value="Calculate">
</form>
</p>

<ul>
<li>Calculator only supports operators +,=,*, and /
<li>Calculator takes both integer and real numbers
<li>Calculator does not support parentheses
<li>Calculator should not display any raw PHP error messages
</ul>

<?php
if($_GET["expression"]){
$invalid = 0;
$exp = $_GET["expression"];

$stripped_exp = preg_replace('/\s+/', ' ', $exp);
if(preg_match('/^[A-Za-z]/', $stripped_exp, $matches)) {
	$invalid = 1;
}
else if (preg_match("/\b\/0\b/", $stripped_exp, $matches)){
	if (!preg_match("/\b\/0.\b/", $stripped_exp, $matches)) {
		$invalid = 1;
	}
}
else if (!(preg_match("/^[0-9]/", $stripped_exp, $matches) || preg_match("/^-/", $stripped_exp, $matches))) {
	$invalid = 1;
}
	
else if (preg_match("#[*+-/][*+/]#", $stripped_exp, $matches)) {
		$invalid = 1;
}

else if (!preg_match("#[0-9]$#", $stripped_exp, $matches)) {
	$invalid = 1;
}

if ($invalid == 0) {
	eval("\$ans = $exp;");
	echo "<h2>Result</h2>"
	.$exp . " = "
	.$ans;
	"<br />";
}
else {
	echo "<h2>Result</h2> Invalid expression inputted " . $exp . "<br />";
}

}
?>

</body>
</html>